<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/1/13
 * Time: 16:16
 */

namespace app\controller\admin;
use app\model\admin\Keyword AS model;

class Keyword extends Base
{

    public function list(){
        $get = input('get.');
        $order = input('order','keyword');
        $asc = input('asc','asc');
        $where = [];
        !empty($get['keyword']) && $where[] = ['keyword','like','%'.$get['keyword'].'%'];
        isset($get['redu']) && $where[] = ['redu','=',$get['redu']];
        $list = model::where($where)
            ->order($order,$asc)
            ->paginate(['page' => $this->page,'list_rwos' => $this->size]);
        return $this->view('/admin/keyword',
            [
                'list' => $list,
                'page' => $list->render()
            ]
        );
    }

    public function saveAll(){
        $keywords = input('keyword');
        foreach ($keywords AS $key => $value){
            $this->save($value);
        }
        return 'OK';
    }

    public function save($keyword = ''){
        $keyword = $keyword ?:input('keyword');
        $keywords = new \app\service\Keyword();
        return $keywords->save($keyword);
    }

    public function del(){
        $ids = input('keyword');
        model::destroy(function($sql) use ($ids){
            $sql->where('keyword','IN',$ids);
        },true);
        return 'OK';
    }

    public function getRedu(){

    }
}